#version 120

//this is insipred by the sources mentioned in openglframework.c
varying vec3 n;
varying vec4 pos;

void main (void)
{
  vec4 color = gl_FrontMaterial.diffuse;
  vec4 matspec = gl_FrontMaterial.specular;
  float shininess = gl_FrontMaterial.shininess;
  vec4 lightspec = gl_LightSource[0].specular;
  vec4 lpos = gl_LightSource[0].position;
  vec3 s = -normalize(pos.xyz - lpos.xyz); 

  vec3 light = s;
   vec3 r = -reflect(light, n);
  r = normalize(r);
  vec3 v = -pos.xyz;
  v = normalize(v);

  vec4 ambient =  gl_LightModel.ambient * gl_FrontMaterial.ambient;;   
  vec4 diffuse  = color * max(0.0, dot(n, s.xyz)) * gl_LightSource[0].diffuse ;
  vec4 specular;
    specular = lightspec * matspec * pow(max(0.0,dot(r, v)), shininess);
    //specular = lightspec * matspec;

//gl_FragColor = ambient;
//gl_FragColor = diffuse;
//gl_FragColor = specular;
gl_FragColor = ambient + diffuse + specular;
//	gl_FragColor = diffuse + specular;
	gl_FragColor = clamp(gl_FragColor, vec4(0,0,0,1), vec4(1,1,1,1));

}
